﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CYTS.Aoyou.Framework.Cache;
using CYTS.Aoyou.Framework.CAInterface;
using CYTS.Aoyou.Framework.DAInterface;
using CYTS.Aoyou.Framework.Database;

namespace CYTS.Aoyou.Framework.Performance.MVCPerformance
{
    public class MVCPerformanceHelper
    {
        private DateTime StartTime;
        private DateTime EndTime;
        private string dataBaseName = "AoYouPerformance";
        public MVCPerformanceHelper()
        {
            StartTime = DateTime.Now;
        }

        /// <summary>
        /// 
        /// </summary>
        public void SetData(MVCPerformanceEntity mvcEntity)
        {
            IPerformanceCacheAdapter perAdapter = CacheAdapterFactory.GetInstance().GetCacheAdapter<IPerformanceCacheAdapter>();
            IPerformanceDataAdapter perdataAdapter = DataAdapterFactory.GetInstance().GetDataAdapter<IPerformanceDataAdapter>();
            mvcEntity.ExecuteTime = functionTime();
            var oldEntity = GetData(mvcEntity.InterFaceName + "/" + mvcEntity.ExecuteFunName);//get redis
            if (mvcEntity.ExecuteTime > 8000)//报警，直接记录mongodb，不累加radis
            {
                mvcEntity.RecordTime = DateTime.Now;
                mvcEntity.AvgExcureTime = mvcEntity.ExecuteTime;
                perdataAdapter.Set<MVCPerformanceEntity>(dataBaseName, "MVCPerformance" + DateTime.Now.ToString("yyyyMMdd"), mvcEntity);
                if (oldEntity == null)
                {
                    mvcEntity.LastRecordTime = DateTime.Now;
                }
            }
            else//记录radis
            {
                if (oldEntity != null)
                {
                    mvcEntity.TotelRecord = oldEntity.TotelRecord + 1;
                    mvcEntity.TotelExcureTime = oldEntity.TotelExcureTime + mvcEntity.ExecuteTime;
                    DateTime dt = DateTime.Now;
                    if ((dt - oldEntity.LastRecordTime).TotalSeconds > 600)//判断时间，记录monogdb，并清空当前radis的累积数据
                    {
                        //调用monogdb方法,算平均时间
                        mvcEntity.AvgExcureTime = mvcEntity.TotelExcureTime / mvcEntity.TotelRecord;
                        mvcEntity.RecordTime = DateTime.Now;
                        perdataAdapter.Set<MVCPerformanceEntity>(dataBaseName, "MVCPerformance" + DateTime.Now.ToString("yyyyMMdd"), mvcEntity);
                        //清空累加值
                        mvcEntity.TotelExcureTime = 0;
                        mvcEntity.TotelRecord = 0;
                        mvcEntity.LastRecordTime = DateTime.Now;
                    }
                    else
                    {
                        mvcEntity.LastRecordTime = oldEntity.LastRecordTime;
                    }
                }
                else //第一次记录radis
                {
                    mvcEntity.TotelRecord = 1;
                    mvcEntity.TotelExcureTime = mvcEntity.ExecuteTime;
                    mvcEntity.LastRecordTime = DateTime.Now;
                }
            }
            perAdapter.Set<MVCPerformanceEntity>(mvcEntity.InterFaceName + "/" + mvcEntity.ExecuteFunName, mvcEntity);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public MVCPerformanceEntity GetData(string key)
        {
            IPerformanceCacheAdapter perAdapter = CacheAdapterFactory.GetInstance().GetCacheAdapter<IPerformanceCacheAdapter>();
            try
            {
                var oldEntity = perAdapter.Get<MVCPerformanceEntity>(key);//get redis
                return oldEntity;
            }
            catch
            {
                return null;
            }
        }


        private double functionTime()
        {
            EndTime = DateTime.Now;
            TimeSpan ts = EndTime - StartTime;
            return ts.TotalMilliseconds;
        }
    }
}
